package com.hongxun.design.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.activitys.BaseActivity;
import com.hongxun.design.activitys.CreateProjectActivity;
import com.hongxun.design.bean.HomeListBean;
import com.hongxun.design.bean.HomeListFunctionBean;
import com.hongxun.design.bean.HomeListProjectStatisticsBean;
import com.hongxun.design.bean.ProjectItemBean;
import com.hongxun.design.bean.photopassbean.SystemTypeBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.IntentToActivity;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.utils.TimeUtil;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.httputil.HttpStringCodeTableUtil;
import com.hongxun.design.utils.imageutil.UriUtil;
import com.hongxun.design.utils.projectdatautil.DataFactory;

import java.util.List;

/**
 * Created by zerdoor_pc on 2016/4/27.
 */
public class HomeRootAdapter extends BaseAdapter {
    private List<HomeListBean> listData;
    private Context context;
    LayoutInflater inflater;

    public HomeRootAdapter(List<HomeListBean> listData, Context context) {
        if (listData.size() == 1) {
            listData.add(new HomeListBean(null, null, null, null, Constant.HOME_LIST_NO));
        }
        this.listData = listData;
        this.context = context;
        inflater = LayoutInflater.from(context);
        projectType = FastJsonUtil.JsonToData_Code(HttpStringCodeTableUtil.projectType);//项目类型
        designStage = FastJsonUtil.JsonToData_Code(HttpStringCodeTableUtil.designStage);//设计阶段
        projectSchedule = FastJsonUtil.JsonToData_Code(HttpStringCodeTableUtil.projectSchedule);//项目类型
        MyLog.L(MyApp.AppName + " listData=" + JSON.toJSONString(listData));
    }

    public void Refresh() {
        //MyLog.L( "HomeFragment Refresh-----刷新函数判断是否有控件=="+listData.size());
        if (listData.size() == 1) {
            listData.add(new HomeListBean(null, null, null, null, Constant.HOME_LIST_NO));
        }
        //MyLog.L( "HomeFragment Refresh-----刷新函数判断是否有控件=="+listData.size());
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int i) {
        return listData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public int getItemViewType(int position) {
        int type = Constant.HOME_LIST_ERROR;
        switch (listData.get(position).getType()) {
            case Constant.HOME_LIST_HEAD:
                type = Constant.HOME_LIST_HEAD;
                break;
            case Constant.HOME_LIST_BODY:
                type = Constant.HOME_LIST_BODY;
                break;
            case Constant.HOME_LIST_NO:
                type = Constant.HOME_LIST_NO;
                break;
            case Constant.HOME_LIST_ERROR:
                type = Constant.HOME_LIST_ERROR;
                break;
        }
        return type;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        synchronized (inflater) {
            int ViewType = getItemViewType(position);
            HeadHolder headHolder = null;
            BodyHolder bodyHolder = null;
            NoHolder noHolder = null;
            if (convertView == null) {
                switch (ViewType) {
                    case Constant.HOME_LIST_HEAD:
                        convertView = inflater.inflate(R.layout.listview_item_home_head, null);
                        headHolder = new HeadHolder(convertView);
                        convertView.setTag(headHolder);
                        break;
                    case Constant.HOME_LIST_BODY:
                        convertView = inflater.inflate(R.layout.listview_item_project, null);
                        bodyHolder = new BodyHolder(convertView);
                        convertView.setTag(bodyHolder);

                        break;
                    case Constant.HOME_LIST_NO:
                        convertView = inflater.inflate(R.layout.listview_item_home_noproject, null);
                        noHolder = new NoHolder(convertView);
                        convertView.setTag(noHolder);
                        break;
                    case Constant.HOME_LIST_ERROR:
                        break;
                }
            } else {
                switch (ViewType) {
                    case Constant.HOME_LIST_HEAD:
                        headHolder = (HeadHolder) convertView.getTag();
                        break;
                    case Constant.HOME_LIST_BODY:
                        bodyHolder = (BodyHolder) convertView.getTag();
                        break;
                    case Constant.HOME_LIST_NO:
                        noHolder = (NoHolder) convertView.getTag();
                        break;
                    case Constant.HOME_LIST_ERROR:
                        break;
                }
            }

            try {
                switch (ViewType) {
                    case Constant.HOME_LIST_HEAD:
                        HomeListBean homeListBean = listData.get(position);
                        List<HomeListFunctionBean> FunctionData = homeListBean.getFunctions();
                        HomeRootHeadGridAdapter rootHeadGridAdapter = new HomeRootHeadGridAdapter(FunctionData, context);
                        headHolder.home_head_functions.setAdapter(rootHeadGridAdapter);//GridView适配
                        break;
                    case Constant.HOME_LIST_BODY:
                        ProjectItemBean project = MyApp.currProject;
                        try {

                            bodyHolder.recent_project_bar.setVisibility(View.VISIBLE);

                            bodyHolder.project_image.setImageURI(UriUtil.getUri(project.getThumb()));
                            bodyHolder.project_name.setText(project.getProject_name());
                            if (project.getIs_exp().equals(MString.code_zero)) {
                                bodyHolder.project_datatype.setVisibility(View.GONE);
                            } else {
                                bodyHolder.project_datatype.setVisibility(View.VISIBLE);
                            }

                            bodyHolder.project_type.setText(getProjectType(project.getProject_type()));
                            bodyHolder.project_statel.setText(getDesignStage(project.getStage()));
                            bodyHolder.project_stater.setText(getProjectSchedule(project.getCurrent_schedule()));
                            SetTypeface.set(bodyHolder.project_right, IconSourse.RIGHTARROW);
                            bodyHolder.project_schedule.setText(TimeUtil.time(project.create_time + "000"));
                            bodyHolder.project_item.setOnClickListener(new MClickListener(position, project) {
                                @Override
                                public void onClick(View view, int position, Object msg) {
                                    ProjectItemBean itemBean = new ProjectItemBean();
                                    itemBean.setBean((ProjectItemBean) msg, 1);
                                    DataFactory.ProjectClick((BaseActivity) context, itemBean);//点击判断函数
                                }
                            });
                            if (project.getFlag().equals(MString.del)) {
                                bodyHolder.delect_icon.setVisibility(View.VISIBLE);
                            } else {
                                bodyHolder.delect_icon.setVisibility(View.GONE);
                            }
                        } catch (Exception e) {
                        }


                        break;
                    case Constant.HOME_LIST_NO:
                        SetTypeface.set(noHolder.noproject_right, IconSourse.RIGHTTOARROW);
                        noHolder.recent_project_item.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (MyApp.userInfo.getOrg_type().equalsIgnoreCase("11")) {
                                    ToastShow.showShort(MyApp.activity, "业主单位人员不能创建项目!");
                                    return;
                                }
                                IntentToActivity.intent(MyApp.activity, CreateProjectActivity.class);
                            }
                        });
                        break;
                    case Constant.HOME_LIST_ERROR:

                        break;
                }
            } catch (Exception e) {
                MyLog.L(MyApp.AppName + "适配错误：" + e.toString());
            }
            return convertView;

        }
    }

    //头部控件
    public class HeadHolder {
        public GridView home_head_functions;

        HeadHolder(View convertView) {
            home_head_functions = (GridView) convertView.findViewById(R.id.home_head_functions);
        }
    }

    //项目
    public class BodyHolder {
        public TextView recent_project_bar;//最近项目bar
        public SimpleDraweeView project_image;//项目图片
        public TextView project_name;//项目名字
        public TextView project_type;//项目类型
        public TextView project_statel;//项目状态左
        public TextView project_stater;//项目状态右
        public TextView project_right;//右方箭头
        public TextView project_schedule;//项目进度
        public TextView project_datatype;//踩勘数据类型
        public RelativeLayout project_item, delect_icon;//

        BodyHolder(View convertView) {

            project_item = (RelativeLayout) convertView.findViewById(R.id.project_item);
            recent_project_bar = (TextView) convertView.findViewById(R.id.recent_project_bar);
            project_image = (SimpleDraweeView) convertView.findViewById(R.id.project_image);
            project_name = (TextView) convertView.findViewById(R.id.project_name);
            project_type = (TextView) convertView.findViewById(R.id.project_type);
            project_statel = (TextView) convertView.findViewById(R.id.project_statel);
            project_stater = (TextView) convertView.findViewById(R.id.project_stater);
            project_right = (TextView) convertView.findViewById(R.id.project_right);
            project_schedule = (TextView) convertView.findViewById(R.id.project_schedule);
            project_datatype = (TextView) convertView.findViewById(R.id.project_datatype);
            delect_icon = (RelativeLayout) convertView.findViewById(R.id.delect_icon);
        }
    }

    //没有项目
    public class NoHolder {
        private TextView recent_project_bar;
        private TextView noproject_right;
        RelativeLayout recent_project_item;

        NoHolder(View convertView) {
            recent_project_bar = (TextView) convertView.findViewById(R.id.recent_project_bar);
            noproject_right = (TextView) convertView.findViewById(R.id.noproject_right);
            recent_project_item = (RelativeLayout) convertView.findViewById(R.id.recent_project_item);
        }
    }


    List<SystemTypeBean> projectType, designStage, projectSchedule;

    private String getProjectType(String code) {
        if (projectType == null)
            projectType = FastJsonUtil.JsonToData_Code(HttpStringCodeTableUtil.projectType);//项目类型
        for (SystemTypeBean s : projectType) {
            if (s.getTypecode().equals(code)) {
                return s.getTypename();
            }
        }
        return "";
    }

    private String getDesignStage(String code) {
        if (designStage == null)
            designStage = FastJsonUtil.JsonToData_Code(HttpStringCodeTableUtil.designStage);//设计阶段
        for (SystemTypeBean s : designStage) {
            if (s.getTypecode().equals(code)) {
                return s.getTypename();
            }
        }
        return "";
    }

    private String getProjectSchedule(String code) {
        if (projectSchedule == null)
            projectSchedule = FastJsonUtil.JsonToData_Code(HttpStringCodeTableUtil.projectSchedule);//项目类型
        for (SystemTypeBean s : projectSchedule) {
            if (s.getTypecode().equals(code)) {
                return s.getTypename();
            }
        }
        return "";
    }
}
